import React, { useEffect } from 'react'
import DocumentTitle from 'react-document-title'
import { ComponentLoading } from 'components/loading'
import { payRet, payReturn } from '@api/submit'
import { selectedAddressAction } from '@store/actionCreators'
import { useHistory, useParams } from 'react-router-dom'
import { useDispatch } from 'react-redux'
import { updateOrder } from '../../../store/actionCreators'



const HandelPay = (props) => {
    const history = useHistory()
    const dispatch = useDispatch()
    const params = useParams()
    const { caigouOrderId, tuanid } = params

    useEffect(() => {

        localStorage.removeItem('fullLoad') // 删除满载标志
        dispatch(updateOrder({
            orderno: null,
            acceptableTime: null,
            loadage: null,
            remark: '',
            isRead: false,
            agree: false
        })) // 清除订单配置缓存

        let prepayOrder = JSON.parse(localStorage.getItem('prepayOrder'))
        let orderNoList = JSON.parse(localStorage.getItem('orderNoList'))

        console.log(prepayOrder, orderNoList)

        let params = {
            orderno: orderNoList[0],
            action: 'orderQuery'
        }

        if (prepayOrder.length === 0) {
            console.log('尾款支付===')
        }

        if (prepayOrder.length > 1) {
            // 预付款 校验两次
            payRet(params).then(res => {

                console.log(res)
                params.orderno = orderNoList[1]

                payRet(params).then(res => {

                    let payReturnParams = {
                        res: res.data,
                        id: caigouOrderId,
                        tuan_id: tuanid,
                        prepay_order: prepayOrder
                    }
                    payReturn(payReturnParams).then(res2 => {
                        if (res2.status === 200) {
                            dispatch(selectedAddressAction({})) // 支付成功清除 选择地址
                            const { price, success, tuan_id } = res2.data
                            console.log(res2.data)
                            if( success === 1 ){  // 成团， 跳转到支付成功
                                history.push(`/paysuccess/${caigouOrderId}/${price}`)
                            }
                            if ( success === 0){ // 未成团， 跳转到团分享
                                history.push(`/group/${tuan_id}`)
                            }
                        } else {
                            dispatch(selectedAddressAction({})) // 支付失败清除 选择地址
                            history.push('/payfail')
                        }
                    })
                })
            })

        } else {
            // 非预付款
            payRet(params).then(res => {

                let payReturnParams = {
                    res: res.data,
                    id: caigouOrderId,
                    tuan_id: tuanid,
                    prepay_order: prepayOrder
                }
                payReturn(payReturnParams).then(res2 => {
                    if (res2.status === 200) {
                        console.log(res2.data)
                        dispatch(selectedAddressAction({})) // 支付成功清除 选择地址
                        localStorage.removeItem('orderNoList')
                        localStorage.removeItem('prepayOrder')

                        history.push(`/paysuccess/${caigouOrderId}/${res2.data}`)

                    } else {
                        dispatch(selectedAddressAction({})) // 支付失败清除 选择地址
                        localStorage.removeItem('orderNoList')
                        localStorage.removeItem('prepayOrder')
                        history.push('/payfail')
                    }
                })

            })

        }

    }, [])

    return (
        <div>
            <DocumentTitle title="账单处理中...." />
            <ComponentLoading />
        </div>
    )
}

export default HandelPay